﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using YiDingCanWp7.Models;
using Newtonsoft.Json;

namespace YiDingCanWp7
{
    public partial class ShopCommentsPage : PhoneApplicationPage
    {
        string shopid = string.Empty;
        ShopReviewData reviews = new ShopReviewData();
        int pageindex = 1;
        int pagesize = 20;
        WebClient webClient = new WebClient();
        public ShopCommentsPage()
        {
            InitializeComponent();

        }

        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                reviews = JsonConvert.DeserializeObject<ShopReviewData>(e.Result);
                ListBoxReviews.ItemsSource = reviews.rows;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                performanceProgressBar.IsIndeterminate = false;
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            shopid = NavigationContext.QueryString["shopid"];
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            performanceProgressBar.IsIndeterminate = true;
            webClient.DownloadStringAsync(new Uri(BaseConfig.BaseUrl + "/Shop/GetShopReviews?shopid=" + shopid + "&pageindex=" + pageindex.ToString() + "&pagesize=" + pagesize.ToString()));
            base.OnNavigatedTo(e);
        }

        private void appbar_AddComment_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddComment.xaml?shopid=" + shopid, UriKind.RelativeOrAbsolute));
        }
    }
}